package denglu.org;

import java.io.IOException;
import java.util.Date;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.httpclient.HttpException;
import org.junit.Test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.bind.annotation.PathVariable;  

import com.entity.Userinfo;


@Controller//表示此类是控制器
@RequestMapping("/register")//注册
public class registerapi {
	    private static final String VALIDATE_PHONE_CODE = "VALIDATE_PHONE_CODE";  //有效的手机验证码
	    private static final String VALIDATE_PHONE = "VALIDATE_PHONE";  //有效的手机号
	    private static final String SEND_CODE_TIME = "SEND_CODE_TIME";  //有效的手机验证码时间	   
	    private  SendMsg_webchinese sendMsg_webchinese;//发送短信的方法对象
	    private static final String photocode ="photocode";//本地生成的图片验证码 
	      
	      
	      
	@Test
	@RequestMapping(value="/randomnum.do",method = RequestMethod.GET)//获取客户端输入的验证码与数据库中的比较后返回响应
	@ResponseBody
	public Object randomback(@RequestBody Userinfo Userinfo){    
	
		return 1;
				
	}
	
	@Test
    @RequestMapping(value="/sendCode.do")  //客户端获取验证码
    @ResponseBody  
    protected void sendCode(String phone, HttpServletRequest request) throws HttpException, IOException {   
        int code=(int)(Math.random()*9000)+1000; //4位验证码
		System.out.println(code);
        
        HttpSession session = request.getSession();  //将验证码存到session中
        session.setAttribute(VALIDATE_PHONE, phone);  
        session.setAttribute(VALIDATE_PHONE_CODE, code);  
        session.setAttribute(SEND_CODE_TIME, new Date().getTime());  
        String smsText = "验证码：" + code;  
        
        System.out.println("手机号：" + phone + ", " + smsText);  
        sendMsg_webchinese.sendMessage(phone, smsText);  //发送验证码到手机
    }  
      
    
    @Test
    @RequestMapping(value="/validate.do")  //验证手机验证码是否正确
    @ResponseBody  
    protected String validate(HttpServletRequest request){  
        HttpSession session = request.getSession();  
        String code = (String) session.getAttribute(VALIDATE_PHONE_CODE);  
        String phone = (String) session.getAttribute(VALIDATE_PHONE);  
        String inputCode = request.getParameter("code");  
        String inputPhone = request.getParameter("phone");  
        if(phone.equals(inputPhone) && code.equalsIgnoreCase(inputCode)){  //比较输入的验证码与session中是否相同
            return "success";  
        }else{  
            return "failure";  
        }  
    }  

	
	
@Test
@RequestMapping(value="/photocode.do")  //比较图片验证码
@ResponseBody  
public String photovalid(){
	
	
	
	return null;
	
}
	
@Test
@RequestMapping(value="/photocodeget.do")  //客户端获取获取图片验证码
@ResponseBody  
public String photoget(HttpServletRequest request){
	 HttpSession session = request.getSession(); 
	
	
	return null;
	
}



}
	
	
	
	
	
	
	
	
	
	


